package com.aididi.concurrent;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Auther: zx.xu
 * @Description:
 */
public class CustomThreadPoolExcutor {

    private ThreadPoolExecutor pool = null;

    public ThreadPoolExecutor getCustomThreadPoolExcutor(){
        return this.pool;
    }

    public void init(){
        pool = new ThreadPoolExecutor(1, 3 , 30 , TimeUnit.MINUTES,
                new ArrayBlockingQueue<>(3), new CustomThreadFactory(),
                new CustomRejectedExecutionHandler());
    }

    public void destory(){
        if(pool != null){
            pool.shutdownNow();
        }
    }


    private class CustomThreadFactory implements ThreadFactory {
        private AtomicInteger count =  new AtomicInteger(0);


        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r);
            String threadName = CustomThreadPoolExcutor.class.getSimpleName()+ count.addAndGet(1);
            System.out.println(threadName);
            thread.setName(threadName);
            return thread;
        }
    }

    //阻塞
    private class CustomRejectedExecutionHandler implements RejectedExecutionHandler {


        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
//            记录异常，报警处理等
//            System.out.println("error.....");
            System.out.println("队列满了，而且线程数量达到了最大数量啦");
            try {
                //让被拒绝的任务重新入队
                executor.getQueue().put(r);
                System.out.println("卡住了吗？？");
            } catch (InterruptedException e) {
                System.out.println("塞入队列异常了");
                //保持线程的终端状态
                Thread.currentThread().interrupt();
            }
            System.out.println("入队ok");
        }

    }

    public static void main(String[] args) {
        CustomThreadPoolExcutor customThreadPoolExcutor = new CustomThreadPoolExcutor();
        customThreadPoolExcutor.init();
        ThreadPoolExecutor pool = customThreadPoolExcutor.getCustomThreadPoolExcutor();

        for (int i = 0; i < 10; i++) {

            System.out.println("提交第"+i+"个任务");
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    work();
                }
            });
            System.out.println("当前队列的剩余容量："+pool.getQueue().remainingCapacity());
        }

        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void work() {
        try{
            System.out.println("线程: " + Thread.currentThread().getName() + "进来啦！");
            Thread.sleep(1000L);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
